<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package    FeatherPHP
 * @subpackage Libs
 * @copyright  Copyright (c) 2006, Julien PORTALIER
 * @link       http://portalier.com/feather
 * @license    http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class Title
{
	const entities = ":['\".#?&/\\\+]:";
	const space = '-';
	
	static function underscore($title, $space=null, $lowercase=true, $entities=null)
	{
		$entities = is_null($entities) ? self::entities : $entities;
		$space    = is_null($space)    ? self::space : $space;
		if ($lowercase) $title = mb_strtolower($title);
		
		$ret = preg_replace($entities, '', $title);
		return preg_replace('/[\s]/', $space, trim($ret));
	}
	
	static function encode($title, $space=null, $entities=null)
	{
		$ret = self::underscore($title, $space, $entities);
		return urlencode($ret);
	}
	
	static function decode($title, $space=null)
	{
		$space = is_null($space) ? self::space : $space;
		$ret = str_replace($space, ' ', urldecode($title));
		return stripslashes($ret);
	}
	
	static function wikiEncode($title)
	{
		return self::encode($title, '_');
	}
	
	static function wikiDecode($title)
	{
		return self::decode($title, '_');
	}
	
	static function urlname($urlname)
	{
		return mb_strtolower(preg_replace(':[^\d\w-_]:', '', $urlname));
	}
	
	static function urlcleanup($title, $lowercase=true, $entities=':([?#&+/]|%\d):')
	{
		if ($lowercase) $title = mb_strtolower($title);
		return preg_replace($entities, '', $title);
	}
}
?>